﻿/*
 * Created by SharpDevelop.
 * User: Michael
 * Date: 04/16/2014
 * Time: 20:06
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Timers;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace AdvGenContact.WPF.Controls
{
	/// <summary>
	/// Description of CollapsibleControl.
	/// </summary>
	public class CollapsibleControl :Expander
	{
		
		private ObservableCollection<AdvGenGrid> sections;
		private  AdvGenGrid ExtendedPanel;
		private System.Windows.Threading.DispatcherTimer dispatcherTimer;
		
		
		public static readonly RoutedEvent AfterExpandedEvent =
			EventManager.RegisterRoutedEvent("AfterExpanded", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CollapsibleControl));
		
		
		public event RoutedEventHandler AfterExpanded
		{
			add { AddHandler(AfterExpandedEvent, value); }
			remove { RemoveHandler(AfterExpandedEvent, value); }
		}
		
			public static readonly RoutedEvent ShowPanelEvent =
			EventManager.RegisterRoutedEvent("ShowPanel", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CollapsibleControl));
		
		
		public event RoutedEventHandler ShowPanel
		{
			add { AddHandler(ShowPanelEvent, value); }
			remove { RemoveHandler(ShowPanelEvent, value); }
		}
		
		
		public static readonly RoutedEvent AfterCollapsedEvent =
			EventManager.RegisterRoutedEvent("AfterCollapsed", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CollapsibleControl));
		
		
		public event RoutedEventHandler AfterCollapsed
		{
			add { AddHandler(AfterCollapsedEvent, value); }
			remove { RemoveHandler(AfterCollapsedEvent, value); }
		}
		
		private double OrginalWidth ;
		/// <summary>
		/// That is the property to set the length of motion effect in (ms)
		/// </summary>
		public static DependencyProperty EffectTimeProperty = DependencyProperty.Register("EffectTime", typeof(int), typeof(CollapsibleControl));
		
		
		
		/// <summary>
		/// That is the property to set the length of motion effect in (ms)
		/// </summary>
		public int EffectTime
		{
			get { return (int)base.GetValue(EffectTimeProperty); }
			set { base.SetValue(EffectTimeProperty, value); }
		}
		
		

		
		/// <summary>
		/// Gets the collection of sections.
		/// </summary>
		[Bindable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public Collection<AdvGenGrid> Sections
		{
			get { return sections; }
			//  private set { sections = value as ObservableCollection<Control>; }
		}
		public CollapsibleControl() :base()
		{
			sections = new ObservableCollection<AdvGenGrid>();
		
			EffectTime = 6;
			
		}
		
		
	
		
		protected override void OnExpanded()
		{
			base.OnExpanded();
			Button btnMax= GetTemplateChild("btnMax" ) as Button;
			if(btnMax!=null){
				btnMax.Visibility= System.Windows.Visibility.Hidden;
			}
			Button btnMin= GetTemplateChild("btnMin" ) as Button;
			if(btnMin != null){
				btnMin.Visibility = System.Windows.Visibility.Visible;
			}
			
		}

		void text1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			int index= ((AdvGenTextBlock)sender).Index;
			FirePanel(index,sender);
			
		}
		
		private void FirePanel(int inx, object sender)
		{
			ContentPresenter cp = GetTemplateChild("PART_ContentPresenter" ) as ContentPresenter;
				Grid myNavigationGrid = GetTemplateChild("myNavigationGrid") as Grid;
				StackPanel inactivePanels = GetTemplateChild("inactivePanels") as StackPanel;
				TextBlock headerText = GetTemplateChild("txtHeader") as TextBlock;
				int i = 0;
				inactivePanels.Children.Clear();
				headerText.Text= sections[inx].HeaderText;
				cp.Content = sections[inx];
				foreach(AdvGenGrid grid in sections)
				{
					if(i!=inx){
						
						AdvGenTextBlock  text1 = new AdvGenTextBlock();
						text1.Text = grid.HeaderText;
						LinearGradientBrush style = this.FindResource("LeftHeaderBrush") as LinearGradientBrush ;
						text1.Background= style;
						text1.Height = 100;
						text1.Index  = i;
						text1.MouseDown += text1_MouseDown;
						inactivePanels.Children.Add(text1);
					}
					i++;
				}
				RaiseEvent(new RoutedEventArgs(ShowPanelEvent,sender));
		}
		
		
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (this.IsInitialized)
			{
				ContentPresenter cp = GetTemplateChild("PART_ContentPresenter" ) as ContentPresenter;
				Grid myNavigationGrid = GetTemplateChild("myNavigationGrid") as Grid;
				StackPanel inactivePanels = GetTemplateChild("inactivePanels") as StackPanel;
				TextBlock headerText = GetTemplateChild("txtHeader") as TextBlock;
				Button btnMax= GetTemplateChild("btnMax" ) as Button;
				Button btnMin= GetTemplateChild("btnMin" ) as Button;
				
				
				cp.Width = ((Panel)this.Parent).Width;
				if(sections.Count >0){
					cp.Content = sections[0];
					headerText.Text = sections[0].HeaderText;
				}
				if(IsExpanded)
				{
					
					btnMax.Visibility= System.Windows.Visibility.Hidden;
					
					btnMin.Visibility = System.Windows.Visibility.Visible;
				}
				else{
					
					btnMax.Visibility= System.Windows.Visibility.Visible;
					
					btnMin.Visibility = System.Windows.Visibility.Hidden;
				}
				btnMin.Click += new RoutedEventHandler(btnMin_Click);
				btnMax.Click += new RoutedEventHandler(btnMax_Click);
				int  i = 0;
				foreach(AdvGenGrid grid in sections)
				{
					if(i>0){
						
						AdvGenTextBlock  text1 = new AdvGenTextBlock();
						text1.Text = grid.HeaderText;
						LinearGradientBrush style = this.FindResource("LeftHeaderBrush") as LinearGradientBrush ;
						text1.Background= style;
						text1.Height = 100;
						text1.Index  = i;
						text1.MouseDown += text1_MouseDown;
						inactivePanels.Children.Add(text1);
					}
					i++;
				}
				
			}
			OrginalWidth = this.ActualWidth;
		}

		void btnMax_Click(object sender, RoutedEventArgs e)
		{
			Expand();
			
			
		}
		
		public void Expand(){
						base.IsExpanded =false;
			base.IsExpanded = true;
			DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
			LinearDoubleKeyFrame ad3 = new LinearDoubleKeyFrame();
			ad3.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0));
			ad3.Value = -this.OrginalWidth;
			LinearDoubleKeyFrame ad = new LinearDoubleKeyFrame();
			ad.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0,0,this.EffectTime));
			ad.Value =0;
			LinearDoubleKeyFrame ad2 = new LinearDoubleKeyFrame();
			ad2.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, this.EffectTime));
			ad2.Value = ActualWidth;


			
			//da.KeyFrames.Add(ad);
			da.KeyFrames.Add(ad3);
			da.KeyFrames.Add(ad);

			Storyboard strbStoryboard = new Storyboard();
			strbStoryboard.Children.Add(da);
					Storyboard.SetTargetProperty(da,    new PropertyPath("RenderTransform.(TranslateTransform.X)"));
			strbStoryboard.Begin(this);
			Button btnMin= GetTemplateChild("btnMin" ) as Button;
			if(btnMin != null){
				btnMin.Visibility = System.Windows.Visibility.Visible;
			}
			Button btnMax= GetTemplateChild("btnMax" ) as Button;
			if(btnMax!=null){
				btnMax.Visibility= System.Windows.Visibility.Hidden;
			}
			
			dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
			dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
			dispatcherTimer.Interval = new TimeSpan(0,0,this.EffectTime);
			dispatcherTimer.Start();
			OnExpanded();
		}
		
		void dispatcherTimer_Tick(object sender, EventArgs e)
		{
			dispatcherTimer.Stop();
			
			RaiseEvent(new RoutedEventArgs(AfterExpandedEvent));
		}

		

		void btnMin_Click(object sender, RoutedEventArgs e)
		{
			base.IsExpanded =false;
			OrginalWidth = this.ActualWidth;
			DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
			LinearDoubleKeyFrame ad3 = new LinearDoubleKeyFrame();
			ad3.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0));
			ad3.Value = 0  ;
			LinearDoubleKeyFrame ad = new LinearDoubleKeyFrame();
			ad.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0,0,this.EffectTime));
			ad.Value =-ActualWidth;
			LinearDoubleKeyFrame ad2 = new LinearDoubleKeyFrame();
			ad2.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, this.EffectTime));
			ad2.Value = ActualWidth;


			
			//da.KeyFrames.Add(ad);
			da.KeyFrames.Add(ad);
da.KeyFrames.Add(ad3);
			Storyboard strbStoryboard = new Storyboard();
			strbStoryboard.Children.Add(da);
			Storyboard.SetTargetProperty(da,    new PropertyPath("RenderTransform.(TranslateTransform.X)"));
			strbStoryboard.Begin(this);
			Button btnMin= GetTemplateChild("btnMin" ) as Button;
			if(btnMin != null){
				btnMin.Visibility = System.Windows.Visibility.Hidden;
			}
			Button btnMax= GetTemplateChild("btnMax" ) as Button;
			if(btnMax!=null){
				btnMax.Visibility= System.Windows.Visibility.Visible;
			}

			dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
			dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick1);
			dispatcherTimer.Interval = new TimeSpan(0,0,this.EffectTime);
			dispatcherTimer.Start();
			//	this.Width=this.MinClosedWidth;
			
		}
		void dispatcherTimer_Tick1(object sender, EventArgs e)
		{
			dispatcherTimer.Stop();
			RaiseEvent(new RoutedEventArgs(AfterCollapsedEvent));
		}

		void internalTimer_Elapsed1(object sender, ElapsedEventArgs e)
		{
			
			RaiseEvent(new RoutedEventArgs(AfterCollapsedEvent));
			
		}
	}
}
